<template>
  <ad-table ref="order"
            :data="tableData" border u-table
            :height="tableHeight" :span-method="objectSpanMethod">
    <u-table-column label="中转次数" prop="transferCount" width="80"/>
    <ad-table-column label="出发地" prop="startCode" queryPro="startName" ctype="city"/>
    <ad-table-column label="目的地" prop="destinationCode" queryPro="destinationName" ctype="city"/>
    <ad-table-column label="参考报价" prop="totalFee" width="100"/>
    <ad-table-column label="利润" prop="profit" width="100"/>
    <ad-table-column label="运输时效" prop="days" width="100"/>
    <ad-table-column label="承运成本" prop="cost" width="100"/>
    <ad-table-column label="供应商" prop="companyName" w="3c2f"/>
    <ad-table-column label="网点城市" prop="siteName" w="3c2f"/>
    <ad-table-column label="联系人" prop="linkman" w="3c2f"/>
    <ad-table-column label="联系电话" prop="linkPhone" ctype="phone-2f"/>
    <u-table-column label="提车费" prop="pick" width="240">
      <template v-slot="{row}">
        <ad-select @change="getPickCost(row,row.$index)" size="mini" style="width: 90px" placeholder="区"
                   v-model="row.pickAreaCode" :options="row.startAreaList"
                   :config="{optionKey: 'name', optionValue: 'code'}"/>
        <el-select @change="getPickCost(row,row.$index)" clearable size="mini" style="width: 90px;margin-left: 2px"
                   v-model="row.pickType"
                   placeholder="类型">
          <el-option :key="'t1'" :label="'人工提'" :value="1"></el-option>
          <el-option :key="'t2'" :label="'小板提'" :value="2"></el-option>
        </el-select>
        <span style="margin-left: 10px">{{row.pickFee ? row.pickFee: '-'}}</span>
      </template>
    </u-table-column>
    <u-table-column label="送车费" prop="take" width="240">
      <template v-slot="{row}">
        <ad-select @change="getTakeCost(row)" size="mini" style="width: 90px" placeholder="区"
                   v-model="row.takeAreaCode" :options="row.destinationAreaList"
                   :config="{optionKey: 'name', optionValue: 'code'}"/>
        <el-select @change="getTakeCost(row)" clearable size="mini" style="width: 90px;margin-left: 2px"
                   v-model="row.takeType"
                   placeholder="类型">
          <el-option :key="'t1'" :label="'人工送'" :value="1"></el-option>
          <el-option :key="'t2'" :label="'小板送'" :value="2"></el-option>
        </el-select>
        <span style="margin-left: 10px">{{row.takeFee ? row.takeFee: '-'}}</span>
      </template>
    </u-table-column>
    <ad-table-column label="添加人" prop="createUserName" w="3c2f"/>
    <ad-table-column label="更新时间" prop="createTime" ctype="datetime"/>
    <ad-table-column label="审核人" prop="orderCreateUser" w="3c2f"/>
    <ad-table-column label="审核时间" prop="orderCreateTime" ctype="datetime"/>
    <ad-table-column label="备注" prop="remark" width="200"/>
  </ad-table>
</template>

<script>
  import AdTable from '@/components/AdTable'
  import AdTableColumn from '@/components/AdTable/AdTableColumn'
  import adTable from '@/mixins/adTable/index'
  import AdSelect from '@/components/AdSelect'
  import api from '@/api/price'

  export default {
    name: "Detailed",
    components: {AdSelect, AdTable, AdTableColumn},
    dicts: ['finance_type', 'finance_customer_subject', 'finance_channel'],
    mixins: [adTable('order')],
    props: {
      startCode: {
        type: String,
        default: null
      },
      destinationCode: {
        type: String,
        default: null
      },
    },

    data() {
      return {
        tableHeight: document.documentElement.clientHeight - 140,
        tableData: []
      }
    },
    created() {
    },
    mounted() {
    },
    methods: {
      searchTable() {
        let self = this;
        if (self.startCode && self.destinationCode)
          api.searchPrice({
            startCode: self.startCode,
            destinationCode: self.destinationCode
          }).then(res => {
            self.tableData = res.data
          })
      },
      //合并行
      objectSpanMethod({row, column, rowIndex, columnIndex}) {
        let self = this;
        switch (column.property) {
          case 'transferCount':
            if (rowIndex === 0 || row.transferId != self.tableData[rowIndex - 1].transferId) {
              let rowspan = 0;
              self.tableData.forEach(element => {
                if (element.transferId === row.transferId) {
                  rowspan++;
                }
              });
              return [rowspan, 1];
            } else {
              return [0, 0];
            }
            break
          case 'totalFee':
            if (rowIndex === 0 || row.transferId != self.tableData[rowIndex - 1].transferId) {
              let rowspan = 0;
              self.tableData.forEach(element => {
                if (element.transferId === row.transferId) {
                  rowspan++;
                }
              });
              return [rowspan, 1];
            } else {
              return [0, 0];
            }
            break
          case 'profit':
            if (rowIndex === 0 || row.transferId != self.tableData[rowIndex - 1].transferId) {
              let rowspan = 0;
              self.tableData.forEach(element => {
                if (element.transferId === row.transferId) {
                  rowspan++;
                }
              });
              return [rowspan, 1];
            } else {
              return [0, 0];
            }
            break
          case 'pick':
            if (rowIndex === 0 || row.transferId != self.tableData[rowIndex - 1].transferId) {
              let rowspan = 0;
              self.tableData.forEach(element => {
                if (element.transferId === row.transferId) {
                  rowspan++;
                }
              });
              return [rowspan, 1];
            } else {
              return [0, 0];
            }
            break
          case 'take':
            if (rowIndex === 0 || row.transferId != self.tableData[rowIndex - 1].transferId) {
              let rowspan = 0;
              self.tableData.forEach(element => {
                if (element.transferId === row.transferId) {
                  rowspan++;
                }
              });
              return [rowspan, 1];
            } else {
              return [0, 0];
            }
            break
        }
      },
      //获取提车费用
      getPickCost(row) {
        let self = this;
        console.log(row)
        if (row.pickType && row.pickAreaCode) {
          api.getPickFee({
            areaCode: row.pickAreaCode,
            pickCarType: row.pickType
          }).then(res => {
            if (row.pickFee) {
              if (row.pickFee != res.data) {
                let fee = Number(res.data) - Number(row.pickFee)
                //计算参考价格
                row.totalFee = Number(row.totalFee) + Number(fee)
                row.pickFee = res.data;
              }
            } else {
              row.pickFee = res.data;
              row.totalFee = Number(row.totalFee) + Number(res.data)
            }
            self.$refs.order.$refs.table.doLayout();
          })
        } else {
          if (row.pickFee) {
            row.totalFee = Number(row.totalFee) - Number(row.pickFee)
          }
          // if (row.takeFee) {
          //   row.totalFee = Number(row.totalFee) + Number(row.takeFee)
          // } else {
          //   row.totalFee = row._row ? row._row.totalFee : row.totalFee;
          // }

          row.pickFee = null
          self.$refs.order.$refs.table.doLayout();
        }
      },
      //获取送车费用
      getTakeCost(row) {
        let self = this;
        if (row.takeType && row.takeAreaCode) {
          api.getTakeFee({
            areaCode: row.takeAreaCode,
            takeCarType: row.takeType
          }).then(res => {
            if (row.takeFee) {
              if (row.takeFee != res.data) {
                let fee = Number(res.data) - Number(row.takeFee)
                //计算参考价格
                row.totalFee = Number(row.totalFee) + Number(fee)
                row.takeFee = res.data;
              }
            } else {
              row.takeFee = res.data;
              row.totalFee = Number(row.totalFee) + Number(res.data)
            }
            self.$refs.order.$refs.table.doLayout();
          })
        } else {
          if (row.takeFee) {
            row.totalFee = Number(row.totalFee) - Number(row.takeFee)
          }
          // if (row.pickFee) {
          //   row.totalFee = Number(row.totalFee) + Number(row.pickFee)
          // } else {
          //   row.totalFee = row._row ? row._row.totalFee : row.totalFee
          // }

          row.takeFee = null
          self.$refs.order.$refs.table.doLayout();
        }
      },
    }
  }
</script>

<style scoped>

</style>